var geo_grid_actionlink = '/index.php?module=surveymodule&action=cat_survey_getjson&ajax_action=1&src=@geolocationmodule';
function preLoadJS(){
	jQuery("#geo_dialog").dialog("close");
	//
	jQuery("#geo_grid").datagrid({
	   url: geo_grid_actionlink,
	   singleSelect: true ,
	   pagination: true ,
	   columns:[[
		{field:'id',title:'ID',width:50},
		{field:'name',title:'Name',width:200},
		{field:'is_active',title:'Active',width:60, formatter: function(value, row, index){
			if (value == "0")
				return "No";
			else
				return "Yes";
		}}
		]],
	   onBeforeLoad : function(param){
			var url = geo_grid_actionlink;
			var options=jQuery(this).datagrid("options");
			
			url+="&page="+options.pageNumber+"&pageSize="+options.pageSize;
			if (options.sortName != null)
			{
				url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
			}
			return url;
	   }
	});
}

var form_onBeforeLoad = function(param)
{
	jQuery(this).datagrid('options').url =_CatManager_BuildUrl() ;
};

var grid_formatter_is_active = function(value, row, index)
{
	if (value == 0)
		return "No";
	else
		return "Yes";
}

var _grid_OnSelect = function(rowIndex, rowData){
	if (rowData.is_active == 0){ // disabled
		jQuery("#ManageUser_TB_Disable").hide();
		jQuery("#ManageUser_TB_Enable").show();
	}
	else
	{
		jQuery("#ManageUser_TB_Disable").show();
		jQuery("#ManageUser_TB_Enable").hide();
	}
}

var _grid_OnUnSelect = function(index, data){
	
}

var _grid_event_detailFormatter = function(index,row){
	return '<div style="padding:2px"><table id="ddv-' + row.id + '"></table></div>';
}

var _grid_event_onExpandRow = function(index,row){
	$('#ddv-'+row.id).datagrid({
		url:'/index.php?module=surveymodule&action=company_getjson_services&src='+__FormHelper_GetSrc()+'&id='+row.id,
		singleSelect:true,
		height:'auto',
		columns:[[
			{field:'id',title:'Category ID',width:70},
			{field:'name',title:'Category name',width:200},
			{field: 'action',title:'Action',width:100, formatter: function(value, row, index){
				return "<a href='javascript:void(0);' onclick='_removeCat("+row.id+");'>Remove</a>";
			}},
		]],
		onResize:function(){
			__FormHelper_GetGridObject().datagrid('fixDetailRowHeight',index);
		},
		onLoadSuccess:function(){
			setTimeout(function(){
				__FormHelper_GetGridObject().datagrid('fixDetailRowHeight',index);
			},0);
		  }
		});
		__FormHelper_GetGridObject().datagrid('fixDetailRowHeight',index);
	}


function _Company_SetState()
{
	var node = __FormHelper_GetGridObject().datagrid('getSelected');
	var temp=Math.random()*9999;
	if (node){
		jQuery.post("/index.php", {
			module: 'surveymodule',
			action: 'company_changestate',
			ajax_action: '1',
			src: __FormHelper_GetSrc(),
			id: node.id,
			t: temp
		},
		function(param)
		{
			__FormHelper_GetGridObject().datagrid('reload');
		}
		);
		__FormHelper_GetGridObject().datagrid('clearSelections');
		jQuery("#ManageUser_TB_Disable").hide();
		jQuery("#ManageUser_TB_Enable").hide();

	}
}

function _CatManager_BuildUrl(){
	var url=__FormHelper_GetActionLink(); 
	var options=__FormHelper_GetGridObject().datagrid("options");
	
	var search = jQuery("#cat_search_name").searchbox("getValue");
	if (search != '')
		url+="&cat_name="+search;
	// for pager
	url+="&page="+options.pageNumber+"&pageSize="+options.pageSize;
	if (options.sortName != null)
	{
		url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
	}
	return url;
} 

function __Button_Add()
{
	var url="/index.php?module=surveymodule&action=company_edit&src="+__FormHelper_GetSrc();
	window.location=url;
}

function __Button_Edit(){
	var node = __FormHelper_GetGridObject().datagrid('getSelected');
	if (node){
		var url="/index.php?module=surveymodule&action=company_edit&id="+node.id+"&src="+__FormHelper_GetSrc();
		window.location=url;
	}
}

function __Button_Remove(){
	var node = __FormHelper_GetGridObject().datagrid('getSelected');
	var msg = "Are you sure deleting this company?";
	if(!confirm(msg)) return;
	var temp=Math.random()*9999;
	if (node){
		jQuery.post("/index.php", {
			module: 'surveymodule',
			action: 'company_delete',
			ajax_action: '1',
			src: __FormHelper_GetSrc(),
			id: node.id,
			t: temp

		},function(param)
		{
			__FormHelper_GetGridObject().datagrid('reload');
		}
		);
		__FormHelper_GetGridObject().datagrid('clearSelections');
	}
}

/**
* Insert Category into Company
*/
function __Button_InsertCat(){
	var node = __FormHelper_GetGridObject().datagrid('getSelected');
	if (node){
		jQuery("#geo_grid").datagrid("clearSelections");
		jQuery("#geo_dialog").dialog("open");
	}
}

function __Button_InsertCat_Action()
{
	var node = __FormHelper_GetGridObject().datagrid('getSelected');
	var temp=Math.random()*9999;
	if (node){
		var node2 = jQuery("#geo_grid").datagrid('getSelected');
		if (node2)
		{
			jQuery.post("/index.php", {
				module: 'surveymodule',
				action: 'company_addcat',
				ajax_action: '1',
				src: __FormHelper_GetSrc(),
				id: node.id,
				cat_id: node2.id,
				t: temp
			},function(param)
				{
					jQuery("#geo_dialog").dialog("close");
					jQuery("#ddv-"+node.id).datagrid('reload');
				}
			);
		}
	}
}

function _removeCat(cat_id)
{
	var node = __FormHelper_GetGridObject().datagrid('getSelected');
	var temp=Math.random()*9999;
	if (node){
		jQuery.post("/index.php", {
				module: 'surveymodule',
				action: 'company_removecat',
				ajax_action: '1',
				src: __FormHelper_GetSrc(),
				id: node.id,
				cat_id: cat_id,
				t: temp
			},function(param)
				{
					jQuery("#ddv-"+node.id).datagrid('reload');
				}
			);
	}
}